package test01springboot.demo.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import test01springboot.demo.entity.Product;
import test01springboot.demo.entity.Takeout;
import test01springboot.demo.service.ProductService;
import test01springboot.demo.service.TakeoutServie;

@Controller
public class ProductController {
	@Resource
	private ProductService productService;
	@Resource
	private TakeoutServie takeoutService;

	@RequestMapping(value = "getList.html")
	public String getList(Model model) {
	List<Product> list=productService.getList();
		model.addAttribute("list", list);
		return "index";
	}

	@RequestMapping(value = "add.html")
	public String addProduct(
			@RequestParam(value = "product", required = true) String product,
			@RequestParam(value = "takeman", required = true) String takeman,
			@RequestParam(value = "productId", required = false) String productId,
			Model model) {
		List<Product> list = productService.getList();
		Takeout takeout = new Takeout();
		takeout.setQuntity(Integer.parseInt(product));
		takeout.setProductId(Integer.parseInt(productId));
		takeout.setHandler(takeman);
		takeoutService.addProduct(takeout);
		int sum = list.get(0).getQuantity();
		Integer newquntity = sum - Integer.parseInt(product);
		takeoutService.updateProduct(newquntity);
		model.addAttribute("list", list);
		return "index";
	}
}
